package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.AdminRoleMapper;
import cn.group.hotel2.mapper.RoleMapper;
import cn.group.hotel2.mapper.RolePermissionMapper;
import cn.group.hotel2.pojo.dto.RoleAddNewDTO;
import cn.group.hotel2.pojo.entity.Role;
import cn.group.hotel2.pojo.entity.RolePermission;
import cn.group.hotel2.pojo.vo.RoleListItemVO;
import cn.group.hotel2.pojo.vo.RoleStandardVO;
import cn.group.hotel2.service.IRoleService;
import cn.group.hotel2.utils.PageResult;
import cn.group.hotel2.utils.PageUtil;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理角色业务的实现类
 *
 * @author Edgar
 * @version 1.0.0
 * @date 2022/11/28
 */
@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public PageResult list(PageUtil pageUtil) {
        List<RoleListItemVO> roleList = roleMapper.getRoleList(pageUtil);
        int total = roleMapper.count();
        PageResult pageResult = new PageResult(roleList,total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public void deleteByRoleId(Long id) {
        RoleStandardVO roleStandardVO = roleMapper.getStandardById(id);
        if (roleStandardVO==null){
            String message = "删除失败,此角色不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = adminRoleMapper.getCountByRoleId(id);
        if (rows>0){
            String message = "当前角色以关联用户,无法删除";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        rows = roleMapper.deleteById(id);
        if (rows!=1){
            String message = "删除失败,服务器繁忙请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }

    @Override
    public void addNew(RoleAddNewDTO roleAddNewDTO) {
        /*Long[] permissionIds = roleAddNewDTO.getPermissionIds();
        log.debug("{}",permissionIds);
        Role role = new Role();
        BeanUtils.copyProperties(roleAddNewDTO,role);
        int rows = roleMapper.insert(role);
        if (rows!=1){
            String message = "添加失败,服务器繁忙请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        Long roleId = role.getId();
        log.debug("角色Id:{}",roleId);
        ArrayList<RolePermission> rolePermissions = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissions.add(rolePermission);
        }
        log.debug("角色权限信息集合:{}",rolePermissions );
        rows = rolePermissionMapper.insertBatch(rolePermissions);
        if (rows!=rolePermissions.size()){
            String message = "添加失败,服务器繁忙请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }*/
    }
}
